package com.sweetest.luffy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sweetest.luffy.entity.Address;
import com.sweetest.luffy.page.Result;
import com.sweetest.luffy.service.IAddressService;
import com.sweetest.luffy.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "用户地址")
public class AddressController {

    @Resource
    private IAddressService addressService;

    @GetMapping(value = "/list")
    @CrossOrigin
    @ApiOperation(value = "地址列表")
    public Result<Object> advertList(HttpServletRequest request) {
        Integer id = JwtUtil.getUserId(request);
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId, id);
        List<Address> list = addressService.list(queryWrapper);
        return Result.ok(list);
    }

    @PostMapping(value = "/save")
    @CrossOrigin
    @ApiOperation(value = "保存")
    public Result<Object> advertList(HttpServletRequest request, @RequestBody Address address) {

        if (null == address.getId()) {
            addressService.updateById(address);
        } else {
            addressService.save(address);
        }

        return Result.ok(address);
    }
}
